/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.serverpackets;

import com.aionengine.gameserver.model.gameobjects.HouseObject;
import com.aionengine.gameserver.model.gameobjects.NpcObject;
import com.aionengine.gameserver.model.gameobjects.UseableItemObject;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.house.House;
import com.aionengine.gameserver.network.aion.AionConnection;
import com.aionengine.gameserver.network.aion.AionServerPacket;

/**
 * @author Rolandas
 */
public class SM_HOUSE_OBJECT extends AionServerPacket {

    HouseObject<?> houseObject;

    public SM_HOUSE_OBJECT(HouseObject<?> owner) {
        this.houseObject = owner;
    }

    @Override
    protected void writeImpl(AionConnection con) {
        Player player = con.getActivePlayer();
        if (player == null)
            return;

        House house = houseObject.getOwnerHouse();
        int templateId = houseObject.getObjectTemplate().getTemplateId();

        writeD(house.getAddress().getId()); // if painted 0 ?
        writeD(house.getOwnerId()); // player which owns house
        writeD(houseObject.getObjectId()); // <outlet[X]> data in house scripts
        writeD(houseObject.getObjectId()); // <outDB[X]> data in house scripts (probably DB id), where [X] is number

        writeD(templateId);
        writeF(houseObject.getX());
        writeF(houseObject.getY());
        writeF(houseObject.getZ());
        writeH(houseObject.getRotation());

        writeD(player.getHouseObjectCooldownList().getReuseDelay(houseObject.getObjectId()));
        if (houseObject.getUseSecondsLeft() > 0)
            writeD(houseObject.getUseSecondsLeft());
        else
            writeD(0);

        Integer color = null;
        if (houseObject != null)
            color = houseObject.getColor();

        if (color != null && color > 0) {
            writeC(1); // Is dyed (True)
            writeC((color & 0xFF0000) >> 16);
            writeC((color & 0xFF00) >> 8);
            writeC((color & 0xFF));
        } else {
            writeC(0); // Is dyed (False)
            writeC(0);
            writeC(0);
            writeC(0);
        }
        writeD(0); // expiration as for armor ?

        byte typeId = houseObject.getObjectTemplate().getTypeId();
        writeC(typeId);

        switch (typeId) {
            case 1: // Use item
                ((UseableItemObject) houseObject).writeUsageData(getBuf());
                break;
            case 7: // Npc type
                NpcObject npcObj = (NpcObject) houseObject;
                writeD(npcObj.getNpcObjectId());
            default:
                break;
        }
    }

}
